package androidApplication;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import capstone.aw_capstone_androidapplication.R;
import android.app.Activity;
import android.content.Intent;

public class DeviceList extends Activity{
	private ListView lstDevices;
	private Commons common;
	
	public DeviceList(){
		
	}
	public void onCreate(Bundle b){
		super.onCreate(b);
		setContentView(R.layout.devicelist);
		common = (Commons) getIntent().getSerializableExtra("Commons");
		System.out.println(common + "COMMON");
		lstDevices = (ListView) findViewById(R.id.lstDevices);
		//Fill list with list of DeviceNames, value as macAddress
		
		lstDevices.setAdapter(new BaseAdapter(){
			public int getCount(){
				return getDeviceNames().length;
			}
			
			public Object getItem(int position){
				return getDeviceNames()[position];
			}
			
			public long getItemId(int position){
				return position;
			}

			public View getView(int position, View arg1, ViewGroup arg2) {
				LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
				View view = inflater.inflate(R.layout.list_row, null);
				TextView tview = (TextView) view.findViewById(R.id.TextView01);
				tview.setText(getDeviceNames()[position]);
				return view;
			}
		});
		
		
		lstDevices.setOnItemLongClickListener( new OnItemLongClickListener(){
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				String uuid = (String) lstDevices.getItemAtPosition(arg2);
				String ip = common.getCsClient().getDevice(uuid, false).getIpAddress();
				Intent intent = new Intent(DeviceList.this.getApplication(), DeviceOverview.class);
				intent.putExtra("Commons", common);
				intent.putExtra("target", ip);
				startActivity(intent);
				return false;
			}			
		});
		
	}
	private String[] getDeviceNames(){
//		ArrayList<String> names = new ArrayList<String>();
////		names.add("Device One");
////		names.add("Device Two");
////		names.add("Device Three");
//		for(String s : common.getThisDevice().getConnectedDevices()){
//			names
//		}
		System.out.println("FART");
		System.out.println(common.getThisDevice());
		System.out.println(common.getThisDevice().getConnectedDevices().size());
		ArrayList<String> names = common.getThisDevice().getConnectedDevices();
		System.out.println(names.size());
		for(String s : names){
			System.out.println(s + "SLOOP");
		}
		String[] temp = new String[names.size()];
		for(int i = 0; i < names.size(); i++){
			temp[i] = names.get(i);
		}
//		String[] tempNames = (String[]) names.toArray();
		
//		setContentView(R.layout.devicenamelistrow);
//		ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.id.TextView01, temp);
		return temp;
	}
	
	
}
